<?php
/**
 * Created by PhpStorm.
 * User: zcq
 * Date: 2018/10/15
 * Time: 9:23
 */
namespace app\admin\service;


use app\admin\model\Vote;
use app\admin\model\VoteData;
use app\admin\model\VoteItems;
use app\admin\model\VoteRule;
use app\admin\model\VoteWhite;
use think\Db;
use think\Exception;
use think\Session;
use think\Validate;

class VoteService extends BaseService
{
    public $model;
    public $ruleModel;
    public $recordModel;
    public $whiteModel;
    public $itemsModel;
    public function __construct()
    {
        parent::__construct();
        $this->model = new Vote();
        $this->recordModel = new VoteData();
        $this->whiteModel = new VoteWhite();
        $this->itemsModel = new VoteItems();
    }

    public function getPaginate($keyword)
    {
        $admin = Session::get('admin');
        $where = ['site_id' => $admin['site_id']];
        if ($keyword){
            $where['name|act_id'] = ['like','%'.$keyword.'%'];
        }

        $data['list'] =  $this->model
            ->where($where)
            ->order('id desc')
            ->paginate();
        $data['page'] = $data['list']->render();
        return $data;
    }

    public function save($params)
    {
        $admin = Session::get('admin');
        $validate = new Validate($this->model->rules);
        if(!$validate->check($params))
            return dataReturn(0,$validate->getError());
        $data = [
            'name' => $params['name'],
            'act_id' => $params['act_id']??'',
            'site_id' => $admin['site_id'],
            'info'=>$params['info'],
            'head_img'=>$params['head_img']??'',
            'detail_name'=>$params['detail_name']??'',
            'enable' => (int)$params['enable']?1:0,
            'type_enable' => (int)$params['type_enable']?1:0,
            'remark' => $params['remark']??'',
            'start' => strtotime($params['start']),
            'end' => strtotime($params['end']),
            'vote_type' => (int)$params['vote_type'],
            'nums' => (int)$params['nums'],
            'per_nums' => (int)$params['per_nums'],
            'comment_type' => (int)$params['comment_type'],
            'click_nums' => (int)$params['click_nums'],
            'is_ip' => (int)$params['is_ip']?1:0,
            'ip_nums' => (int)$params['ip_nums'],
            'is_device' => (int)$params['is_device']?1:0,
            'device_nums' => (int)$params['device_nums'],
            'enable_web' => (int)$params['enable_web'],
            'search_words' => $params['search_words'],
            'vote_btn_name'=>$params['vote_btn_name']??'投票',
            'modtime'=>time(),
        ];
        if($params['id']){
            $res = $this->model->where('id',$params['id'])->update($data);

        }else{
            $data['addtime'] = time();
            $res = $this->model->insert($data);
        }
        if($res){
            //清除redis缓存
            $this->clearVoteCache($params['id']);
            return dataReturn(1,'保存成功');
        }else{
            return dataReturn(0,'保存失败');
        }
    }

    public function check($id)
    {
        $data = $this->model->where('id',$id)->find();
        if($data){
            $data['start'] = date('Y-m-d H:i',$data['start']);
            $data['end'] = date('Y-m-d H:i',$data['end']);
        }
        return $data;
    }



    public function getWhite($voteId)
    {
        return $this->whiteModel->where('vote_id',$voteId)->order('id desc')->select();
    }

    /**
     * @param $params
     */
    public function addWhite($params)
    {
        $validate = new Validate($this->whiteModel->rules);
        if(!$validate->check($params))
            return dataReturn(0,$validate->getError());
        if(!preg_match(config('mobile_reg'),$params['mobile'])){
            return dataReturn(0,'手机号码格式不正确');
        }
        $a = $this->whiteModel
            ->where('vote_id',$params['id'])
            ->where('mobile',$params['mobile'])
            ->find();
        if($a)
            return dataReturn(0,'电话号码已存在');

        $data = [
            'vote_id' => $params['id'],
            'mobile' => $params['mobile'],
            'nums' => $params['nums'],
            'per_nums' =>$params['per_nums'],
            'click_nums' =>$params['click_nums'],
            'addtime'=>time(),
            'modtime'=>time(),
        ];
        $res = $this->whiteModel->insert($data);
        if($res){
            //清除白名单缓存
            $this->clearVoteWhite($params['id']);
            return dataReturn(1,'保存成功');
        }else{
            return dataReturn(0,'保存失败');
        }
    }

    /**
     * @param $params
     */
    public function WhiteUpdate($params)
    {
        $white = $this->whiteModel->find($params['white_id']);
        $validate = new Validate($this->whiteModel->rules);
        if(!$validate->check($params))
            return dataReturn(0,$validate->getError());
        if(!preg_match(config('mobile_reg'),$params['mobile'])){
            return dataReturn(0,'手机号码格式不正确');
        }
        $a = $this->whiteModel
            ->where('vote_id',$params['id'])
            ->where('mobile',$params['mobile'])
            ->where('id', 'neq', $params['white_id'])
            ->find();
        if($a)
            return dataReturn(0,'电话号码已存在');

        $data = [
            'mobile' => $params['mobile'],
            'nums' => $params['nums'],
            'per_nums' =>$params['per_nums'],
            'click_nums' =>$params['click_nums'],
            'modtime'=>time(),
        ];
        $res = $this->whiteModel->where('id',$params['white_id'])->update($data);
//        $res = $white->update($data);
        if($res){
            //清除白名单缓存
            $this->clearVoteWhite($params['id']);
            return dataReturn(1,'保存成功');
        }else{
            return dataReturn(0,'保存失败');
        }
    }

    public function updateWhite($id,$field,$value)
    {
        $white = $this->whiteModel->find($id);
        if($field == 'mobile'){
            $b = $this->whiteModel
                ->where('vote_id',$white['vote_id'])
                ->where('mobile',$value)
                ->where('id','neq',$id)
                ->find();
            if($b)
                return dataReturn(0,'电话号码已存在');
        }
        $data[$field] = $value;
        $data['modtime'] = time();
        $res = $this->whiteModel->where('id',$id)->update($data);
        if($res){
            //清除白名单缓存
            $this->clearVoteWhite($white['vote_id']);
            return dataReturn(1,'保存成功');
        }else{
            return dataReturn(0,'保存失败');
        }
    }

    public function delWhite($id)
    {
        $white = $this->whiteModel->find($id);
        if(!$white)
            return dataReturn(0,'数据不存在');

        $res = $this->whiteModel->where('id',$id)->delete();
        if($res){
            //清除白名单缓存
            $this->clearVoteWhite($white['vote_id']);
            return dataReturn(1,'删除成功');
        }else{
            return dataReturn(0,'删除失败');
        }
    }
}